package logic;

public class Piece {

	private int color;
	public static final int COLOR_RED = 0;
	public static final int COLOR_GREEN = 1;

	private int type;
	public static final int TYPE_ROOK = 1;
	public static final int TYPE_HOURSE = 2;
	public static final int TYPE_ELEPHANT = 3;
	public static final int TYPE_GUARD = 4;
	public static final int TYPE_KING = 5;
	public static final int TYPE_CANNON = 6;
	public static final int TYPE_PAWN = 7;

	private int row;

	public static final int ROW_1 = 0;
	public static final int ROW_2 = 1;
	public static final int ROW_3 = 2;
	public static final int ROW_4 = 3;
	public static final int ROW_5 = 4;
	public static final int ROW_6 = 5;
	public static final int ROW_7 = 6;
	public static final int ROW_8 = 7;
	public static final int ROW_9 = 8;
	public static final int ROW_10 = 9;

	private int column;

	public static final int COLUMN_A = 0;
	public static final int COLUMN_B = 1;
	public static final int COLUMN_C = 2;
	public static final int COLUMN_D = 3;
	public static final int COLUMN_E = 4;
	public static final int COLUMN_F = 5;
	public static final int COLUMN_G = 6;
	public static final int COLUMN_H = 7;
	public static final int COLUMN_I = 8;

	private boolean isCaptured = false;
	private boolean passRiver = false;

	public Piece(int color, int type, int row, int column) {
		this.color = color;
		this.type = type;
		this.row = row;
		this.column = column;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getColumn() {
		return column;
	}

	public void setColumn(int column) {
		this.column = column;
	}

	public void isCaptured(boolean isCaptured) {
		this.isCaptured = isCaptured;
	}

	public boolean isCaptured() {
		return isCaptured;
	}

	public static String getRowString(int row) {
		String strRow = "unknown";
		switch (row) {
		case ROW_1:
			strRow = "1";
			break;
		case ROW_2:
			strRow = "2";
			break;
		case ROW_3:
			strRow = "3";
			break;
		case ROW_4:
			strRow = "4";
			break;
		case ROW_5:
			strRow = "5";
			break;
		case ROW_6:
			strRow = "6";
			break;
		case ROW_7:
			strRow = "7";
			break;
		case ROW_8:
			strRow = "8";
			break;
		case ROW_9:
			strRow = "9";
			break;
		case ROW_10:
			strRow = "10";
			break;
		}
		return strRow;
	}

	public static String getColumnString(int column) {
		String strCol = "unknown";
		switch (column) {
		case COLUMN_A:
			strCol = "A";
			break;
		case COLUMN_B:
			strCol = "B";
			break;
		case COLUMN_C:
			strCol = "C";
			break;
		case COLUMN_D:
			strCol = "D";
			break;
		case COLUMN_E:
			strCol = "E";
			break;
		case COLUMN_F:
			strCol = "F";
			break;
		case COLUMN_G:
			strCol = "G";
			break;
		case COLUMN_H:
			strCol = "H";
			break;
		case COLUMN_I:
			strCol = "I";
			break;
		}
		return strCol;
	}

	public void passRiver(boolean pass) {
		this.passRiver = pass;
	}

	public boolean passRiver() {
		return passRiver;
	}

	public String getTypeString() {
		switch (type) {
		case TYPE_CANNON:
			return "Cannon";
		case TYPE_ELEPHANT:
			return "Elephant";
		case TYPE_GUARD:
			return "Guard";
		case TYPE_HOURSE:
			return "Hourse";
		case TYPE_KING:
			return "King";
		case TYPE_PAWN:
			return "Pawn";
		case TYPE_ROOK:
			return "Rook";
		default:
			throw new IllegalStateException("unknown type");
		}
	}

	public String getColorString() {
		if (this.getColor() == COLOR_GREEN) {
			return "Green";
		} else {
			return "Red";
		}
	}

	public String toString() {
		return this.getColorString() + ":" + this.getTypeString() + " at "
				+ this.getColumnString(getColumn()) + "/"
				+ this.getRowString(getRow());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + color;
		result = prime * result + column;
		result = prime * result + (isCaptured ? 1231 : 1237);
		result = prime * result + (passRiver ? 1231 : 1237);
		result = prime * result + row;
		result = prime * result + type;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Piece other = (Piece) obj;
		if (color != other.color)
			return false;
		if (column != other.column)
			return false;
		if (isCaptured != other.isCaptured)
			return false;
		if (passRiver != other.passRiver)
			return false;
		if (row != other.row)
			return false;
		if (type != other.type)
			return false;
		return true;
	}

}
